home *** CD-ROM | disk | FTP | other *** search
Text File | 1990-06-08 | 3.1 KB | 75 lines | [TEXT/GEOL] |
- Item 5861365 4-June-90 11:04PDT
-
- From: ISAACSON Isaacson, Trygve
-
- To: D4202 HLH Assoc, Seth Haberman,PRT
- CPLUS.DEV$ C++ Interest List--Developers
- CPLUS.APPLE$ C++ Interest List--Apple Employees
-
- Sub: Re> c++ & the heap
-
- Scot,
-
- C++ doesn't really change the face of the Mac. You can still use
- non-relocatable memory (resulting in heap fragmentation, etc., just like
- in the DOS world), or you can use relocatable memory (and worry about memory
- moving underneath your dereferenced handles).
-
- The Good News: if you just use the 'new' operator to create pointer-based
- objects, you don't have to worry about the objects moving. This is because
- 'operator new' uses 'malloc' from the standard C library, and malloc maintains
- its own list of blocks which are allocated with 'NewPtr'.
-
- The Bad News: you <probably> don't want to do this! Malloc is not a very
- friendly companion to the Memory Manager. Your heap may become fragmented and
- you will find that malloc never frees blocks under 2K in size (it assumes it is
- the only one doing the allocation, so it just keeps the free blocks around for
- later use). You can live with this if you don't use the Memory Manager, but if
- you use the Toolbox at all, you will at least indirectly be using the Memory
- Manager, and it may cause you brainaches.
-
- The Solution: derive your objects from a class that doesn't use malloc for
- allocation. Oh look, here's one now (built-in to MPW C++): 'HandleObject'.
- Of course, you will have to worry about HandleObjects moving, but you can put a
- Lock() and Unlock() method in your own superclass you descend from HandleObject
- (see below) and use care to ensure you lock objects at the right times (all the
- traditional memory-moving gotcha situations apply), or I suppose you could make
- the convention that you MoveHHI() and HLock() your objects in their init method
- (but that would be cheating...?).
-
- I suggest reading Andy Shebanow's article in the April issue of Develop if you
- want to know about malloc and the Mac. He gives an example class 'PtrObject'
- which doesn't use malloc but isn't relocatable like HandleObject. In case
- you're interested, here's my ulitimate superclass from which all my objects
- descend (the lock and unlock methods use a counter so I can nest my locks and
- unlocks without getting burned by nested HLock and HUnlock):
-
- class TOrionObject : public HandleObject
- {
- public:
-
- virtual void IOrionObject();
- virtual void Free();
- virtual void Fields(TInspector *inspector);
-
- void Lock() { if (fLockCount++ == 0) HLock((Handle) this); }
- void Unlock() { if (--fLockCount == 0) HUnlock((Handle) this); }
-
- protected:
-
- OSType fID; // each class inits this to its identifier
-
- private:
-
- lhex fLockCount;
- };
-
- If you're using MacApp (it doesn't sound like you are), you're probably better
- off subclassing from TObject (which is handle-based) since it gives you some
- more stuff for free.
-
- Trygve Isaacson
- Orion Network Systems, Inc.
- Link: ISAACSON
-
-